use std::ffi::CString;

#[cfg(unix)]
const NULL_DEV: &str = "/dev/null";
#[cfg(windows)]
const NULL_DEV: &str = "NUL";

/// Silence all console output by redirecting stdout and stderr to the null device.
/// Returns true if redirection succeeded for both streams.
pub fn silence() -> bool {
    unsafe {
        let dev = match CString::new(NULL_DEV) { Ok(s) => s, Err(_) => return false };
        let fd = libc::open(dev.as_ptr(), libc::O_WRONLY);
        if fd < 0 { return false; }
        let mut ok = true;
        if libc::dup2(fd, 1) < 0 { ok = false; }
        if libc::dup2(fd, 2) < 0 { ok = false; }
        libc::close(fd);
        ok
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn silence_returns_a_bool() {
        let res = super::silence();
        assert!(res == true || res == false);
    }
}
